package com.yu.yupicture.controller;

import com.yu.yupicture.Untils.ThrowUntil;
import com.yu.yupicture.annotation.AuthCheck;
import com.yu.yupicture.common.BaseReponse;
import com.yu.yupicture.common.Result;
import com.yu.yupicture.exception.ErrorCode;
import com.yu.yupicture.model.domain.dto.spaceAnalyze.*;
import com.yu.yupicture.model.domain.entry.Space;
import com.yu.yupicture.model.domain.entry.User;
import com.yu.yupicture.model.domain.vo.spaceAnalyze.*;
import com.yu.yupicture.service.SpaceAnalyze;
import com.yu.yupicture.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/space/analyze")
@Api(tags = "空间分析")
public class SpaceAnalyeConreoller {

    @Resource
    private SpaceAnalyze spaceAnalyze;

    @Resource
    private UserService userService;

    /**
     * 获取空间使用状态
     */
    @PostMapping("/usage")
    @ApiOperation("获取空间使用状态")
    public BaseReponse<SpaceUsageAnalyzeResponse> getSpaceUsageAnalyze(
            @RequestBody SpaceAnalyzeRequest spaceAnalyzeRequest,
            HttpServletRequest request
    ) {
        ThrowUntil.throwIf(spaceAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        SpaceUsageAnalyzeResponse spaceUsageAnalyze = spaceAnalyze.spaceUsageAnalyze(spaceAnalyzeRequest, loginUser);
        return Result.success(spaceUsageAnalyze);
    }

    /**
     * 获取空间使用状态
     */
    @PostMapping("/category")
    @ApiOperation("获取空间分类分析")
    public BaseReponse<List<SpaceCategoryAnalyzeResponse>> getSpaceCategoryAnalyze(
            @RequestBody SpaceCategoryAnalyzeRequest spaceAnalyzeRequest,
            HttpServletRequest request
    ) {
        User loginUser = userService.getLoginUser(request);
        ThrowUntil.throwIf(spaceAnalyzeRequest == null||loginUser==null, ErrorCode.PARAMS_ERROR);
        List<SpaceCategoryAnalyzeResponse> spaceCategoryAnalyze = spaceAnalyze.spaceCategoryAnalyze(spaceAnalyzeRequest, loginUser);
        return Result.success(spaceCategoryAnalyze);
    }

    @PostMapping("/tag")
    @ApiOperation("获取空间标签分析")
    public BaseReponse<List<SpaceTagAnalyzeResponse>> getSpaceTagAnalyze(@RequestBody SpaceTagAnalyzeRequest spaceTagAnalyzeRequest, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        ThrowUntil.throwIf(spaceTagAnalyzeRequest == null||loginUser==null, ErrorCode.PARAMS_ERROR);
        List<SpaceTagAnalyzeResponse> resultList = spaceAnalyze.spaceTagAnalyze(spaceTagAnalyzeRequest, loginUser);
        return Result.success(resultList);
    }

    @PostMapping("/size")
    @ApiOperation("获取空间大小分析")
    public BaseReponse<List<SpaceSizeAnalyzeResponse>> getSpaceSizeAnalyze(@RequestBody SpaceSizeAnalyzeRequest spaceSizeAnalyzeRequest, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        ThrowUntil.throwIf(spaceSizeAnalyzeRequest == null||loginUser==null, ErrorCode.PARAMS_ERROR);
        List<SpaceSizeAnalyzeResponse> resultList = spaceAnalyze.spaceSizeAnalyze(spaceSizeAnalyzeRequest, loginUser);
        return Result.success(resultList);
    }

    @PostMapping("/user")
    @ApiOperation("获取用户上传分析")
    public BaseReponse<List<SpaceUserAnalyzeResponse>> getSpaceUserAnalyze(@RequestBody SpaceUserAnalyzeRequest spaceUserAnalyzeRequest, HttpServletRequest request) {
        ThrowUntil.throwIf(spaceUserAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<SpaceUserAnalyzeResponse> resultList = spaceAnalyze.userUploadAnalyze(spaceUserAnalyzeRequest, loginUser);
        return Result.success(resultList);
    }

    @PostMapping("/rank")
    @ApiOperation("获取空间使用排名")
    @AuthCheck(value = "admin")
    public BaseReponse<List<Space>> getSpaceRankAnalyze(@RequestBody SpaceRankAnalyzeRequest spaceRankAnalyzeRequest, HttpServletRequest request) {
        ThrowUntil.throwIf(spaceRankAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        List<Space> resultList = spaceAnalyze.spaceUsageRank(spaceRankAnalyzeRequest, loginUser);
        return Result.success(resultList);
    }




}
